package com.uchengwang.ui.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.uchengwang.functions.http.config.BaseConfig;

/**
 * Created by U城网 on 2017/4/21.
 */

public class SPUtils {
    private static final String FILE_NAME = "sp_ucw";

    public static final String TOKEN = "token";

    private static SharedPreferences sp;
    private static SPUtils utils;
    private static SharedPreferences.Editor editor;

    private SPUtils(){}

    public static SPUtils getPreferences(Context context) {
        if (utils == null){
            utils = new SPUtils();
            sp = context.getApplicationContext().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
            editor = sp.edit();
        }
        return utils;
    }

    /**
     * 判断是否已经打开过app true是 false没有
     * */
    public boolean isOpend(){
        return getBoolean("isFirst");
    }

    /**
     * 是否已经打开过
     * */
    public void setOpened(){
        putBoolean("isFirst", true);
    }

    /**
     * 设置U城网ID
     * */
    public void setAccountId(long uid){
        putLong(BaseConfig.UID, uid);
    }
    public long getAccountId(){
        return getLong(BaseConfig.UID);
    }

    /**
     * 设置U城网注册账号 注册时手机号
     * */
    public void setUserName(String userName){
        putString(BaseConfig.USER_NAME, userName);
    }
    public String getUserName(){
        return getString(BaseConfig.USER_NAME);
    }

    /**
     * 设置U城网注册账号 真实姓名
     * */
    public void setRealName(String realName){
        putString(BaseConfig.REAL_NAME, realName);
    }
    public String getRealName(){
        return getString(BaseConfig.REAL_NAME);
    }

    /**
     * 设置U城网密码
     * */
    public void setAccountPw(String pw){
        putString(BaseConfig.USER_PWD, pw);
    }
    public String getAccountPw(){
        return getString(BaseConfig.USER_PWD);
    }

    /**
     * 存放token
     * */
    public void setToken(String token){
        putString(BaseConfig.TOKEN, token);
    }
    public String getToken(){
        return getString(BaseConfig.TOKEN);
    }

    /**
     * 存放上次token生成时间
     * */
    public void setTokenTime(){
        putLong(BaseConfig.TOKEN_CREATE_TIME, System.currentTimeMillis());
    }
    public long getTokenTime(){
        return getLong(BaseConfig.TOKEN_CREATE_TIME);
    }

    /**
     * 存储U城网的U聊开始时间
     * */
    public void setUCHatTime(long time){
        putLong(BaseConfig.U_CHAT_TIME, time);
    }

    /**
     * 得到U聊开始时间
     * */
    public long getUChatTime(){
        return getLong(BaseConfig.U_CHAT_TIME);
    }

    /**
     * 清空U聊时间
     * */
    public void clearUchatTime(){
        putLong(BaseConfig.U_CHAT_TIME, 0);
    }

    /**
     * 设置 来消息 是否允许通知 true 不允许消息通知  默认false允许
     * */
    public void setMsgNotNotifyAllowed(boolean isAloow){
        putBoolean(BaseConfig.MSG_NOTIFY_ALLOWED, isAloow);
    }

    /**
     * 获取 来消息是否允许 true 不允许消息通知 默认false允许
     * */
    public boolean getMsgNotNotifyAllowed(){
        return getBoolean(BaseConfig.MSG_NOTIFY_ALLOWED);
    }


    /**
     * 存放融云token
     * */
    public void setRyToken(String token){
        putString(BaseConfig.RY_TOKEN, token);
    }
    public String getRyToken(){
        return getString(BaseConfig.RY_TOKEN);
    }

    /**
     * 存放关联账号主token
     * */
    public void setLinkMainToken(String mainToken){
        putString(BaseConfig.LINK_MAIN_TOKEN, mainToken);
    }
    //获取主token
    public String getLinkMainToken(){
        return getString(BaseConfig.LINK_MAIN_TOKEN);
    }

    /**
     * 获取上次进入享聊的时间
     * */
    public long getEnterEnjoyChatTime(){
        return getLong("enter_enjoy_chat_time");
    }
    /**
     * 设置进入享聊的时间
     * */
    public void setEnterEnjoyChatTime(){
        putLong("enter_enjoy_chat_time", System.currentTimeMillis());
    }

    /**
     * 设置享聊心情
     * */
    public int getXiangLiaoType(){
        return getInt("xiangliao_type");
    }
    /**
     * 设置进入享聊的时间
     * */
    public void setXiangLiaoType(int xiangLiaoType){
        putInt("xiangliao_type", xiangLiaoType);
    }



    public void putInt(String key, int value){
        editor.putInt(key, value).commit();
    }

    public void putString(String key, String value){
        editor.putString(key, value).commit();
    }

    public void putLong(String key, long value){
        editor.putLong(key, value).commit();
    }

    public void putBoolean(String key, boolean b){
        editor.putBoolean(key, b).commit();
    }


    public int getInt(String key){
        return sp.getInt(key, 0);
    }

    public String getString(String key){
        return sp.getString(key, "");
    }

    public long getLong(String key){
        return sp.getLong(key, 0);
    }

    public boolean getBoolean(String key){
        return sp.getBoolean(key, false);
    }
}
